package com.woniuxy.joylifecenter.scheduler;

import com.woniuxy.common.entity.CenterLog;
import com.woniuxy.joylifecenter.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;

@Slf4j
@Component
public class LogScheduler {
    //占位符，读取配置文件中Spring EL表达式的值，以确定日志保留天数
    @Value("${scheduler.log.days}")
    private Integer days;
    @Autowired
    private LogService logService;

    /**
     * @return
     * @Author ArmWatt
     * @Date 2020/4/29
     * @Param
     * @Description 记录定时删除日志
     **/

    //cron表达式，每十分钟执行一次
    @Scheduled(cron = "0 0/10 * * * ? ")
    public void autoDeleteLog() {
        Calendar now = Calendar.getInstance();
        //计算得出最近的过期日期
        now.add(Calendar.DAY_OF_YEAR, -days);
        //获取删除记录条数
        int count = logService.deleteLogBySchedule(now.getTime());

        CenterLog centerLog = new CenterLog();
        centerLog.setCenterLogid(UUID.randomUUID().toString().replaceAll("-", ""));
        centerLog.setCenterIp("");
        centerLog.setCenterParams("");
        centerLog.setCenterUsername("定时删除任务");
        centerLog.setCenterCreatetime(new Date());
        centerLog.setCenterMethod("");
        centerLog.setCenterOperation("本次定时任务删除了：" + count + "条日志记录");
        logService.saveLog(centerLog);
    }

}
